package com.harmony.pilot.llm.controller;

import com.harmony.pilot.common.dto.RestResult;
import com.harmony.pilot.llm.mcp.MCPClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * MCP (Model Context Protocol) 控制器
 */
@Slf4j
@RestController
@RequestMapping("/llm/mcp")
@RequiredArgsConstructor
public class MCPController {
    
    private final MCPClient mcpClient;
    
    /**
     * 调用MCP工具
     */
    @PostMapping("/call-tool")
    public RestResult<Map<String, Object>> callTool(
            @RequestParam String toolName,
            @RequestBody Map<String, Object> arguments) {
        log.info("Calling MCP tool: {}", toolName);
        
        try {
            Map<String, Object> result = mcpClient.callTool(toolName, arguments);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to call MCP tool", e);
            return RestResult.error("Failed to call MCP tool: " + e.getMessage());
        }
    }
    
    /**
     * 列出可用的MCP工具
     */
    @GetMapping("/list-tools")
    public RestResult<List<Map<String, Object>>> listTools() {
        log.info("Listing MCP tools");
        
        try {
            List<Map<String, Object>> tools = mcpClient.listTools();
            return RestResult.success(tools);
        } catch (Exception e) {
            log.error("Failed to list MCP tools", e);
            return RestResult.error("Failed to list tools: " + e.getMessage());
        }
    }
    
    /**
     * 增强代码上下文
     */
    @PostMapping("/enhance-context")
    public RestResult<Map<String, Object>> enhanceContext(
            @RequestParam String filePath,
            @RequestBody String code) {
        log.info("Enhancing context for: {}", filePath);
        
        try {
            Map<String, Object> result = mcpClient.enhanceContext(filePath, code);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to enhance context", e);
            return RestResult.error("Failed to enhance context: " + e.getMessage());
        }
    }
    
    /**
     * 分析代码依赖
     */
    @GetMapping("/analyze-dependencies")
    public RestResult<Map<String, Object>> analyzeDependencies(
            @RequestParam String projectRoot,
            @RequestParam String filePath) {
        log.info("Analyzing dependencies for: {}", filePath);
        
        try {
            Map<String, Object> result = mcpClient.analyzeDependencies(projectRoot, filePath);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to analyze dependencies", e);
            return RestResult.error("Failed to analyze dependencies: " + e.getMessage());
        }
    }
    
    /**
     * 查询OpenHarmony API文档
     */
    @GetMapping("/query-oh-api")
    public RestResult<Map<String, Object>> queryOHAPIeDocs(@RequestParam String apiName) {
        log.info("Querying OpenHarmony API docs for: {}", apiName);
        
        try {
            Map<String, Object> result = mcpClient.queryOHAPIDocs(apiName);
            return RestResult.success(result);
        } catch (Exception e) {
            log.error("Failed to query OH API docs", e);
            return RestResult.error("Failed to query API docs: " + e.getMessage());
        }
    }
    
    /**
     * 检查MCP可用性
     */
    @GetMapping("/status")
    public RestResult<Map<String, Object>> getStatus() {
        log.info("Checking MCP status");
        
        try {
            boolean available = mcpClient.isAvailable();
            Map<String, Object> status = Map.of(
                "available", available,
                "message", available ? "MCP server is running" : "MCP server is not available"
            );
            return RestResult.success(status);
        } catch (Exception e) {
            log.error("Failed to check MCP status", e);
            return RestResult.error("Failed to check status: " + e.getMessage());
        }
    }
}

